<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Ticket Status</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript">

        // Initialize the producer select box
        function initFilmSelect() {
            removeAllSelectOptions("film_code");
            addSelectOptions("film_code", "Choose one", "film");
            ajaxCall('getfilms.php', loadFilmsFromXml);
        }

        // Parse the XML file containing the list of producers and add
        // to the producer select box
        function loadFilmsFromXml(xml_doc) {
            xmlDoc=loadXMLString(xml_doc);

            film_list=xmlDoc.documentElement.getElementsByTagName("film");
            for (i=0;i<film_list.length;i++) {
                film_code=film_list[i].getElementsByTagName("code");
                film_name=film_list[i].getElementsByTagName("name");
                try {
                    code = film_code[0].firstChild.nodeValue;
                    name = film_name[0].firstChild.nodeValue;
                    addSelectOptions("film_code", code, name);
                } catch (er) {
                    code = 1;
                    name = '';
                }
            }
            initTicketSelect();
        }

        // Initialize the producer select box
        function initTicketSelect() {
            removeAllSelectOptions("ticket_rate");
            addSelectOptions("ticket_rate", "Choose one", "ticket rate");
            ajaxCall('gettickets.php', loadTicketsFromXml);
        }

        // Parse the XML file containing the list of producers and add
        // to the producer select box
        function loadTicketsFromXml(xml_doc) {
            xmlDoc=loadXMLString(xml_doc);

            ticket_list=xmlDoc.documentElement.getElementsByTagName("ticket");
            for (i=0;i<ticket_list.length;i++) {
                ticket_rate=ticket_list[i].getElementsByTagName("rate");
                coupon_value=ticket_list[i].getElementsByTagName("coupon_value");
                try {
                    rate = ticket_rate[0].firstChild.nodeValue;
                    value = coupon_value[0].firstChild.nodeValue;
                    addSelectOptions("ticket_rate", rate, value);
                } catch (er) {
                    rate = 20;
                    value = '';
                }
            }

        }

        function dispTableHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            table_text = "<button onclick='displayTable()'>Show all tickets for this film</button>";
            table_text = table_text + "<table border='1'>"+
                      "<tr><th>ID</th>"+
                          "<th>Customer Name</th>"+
                          "<th>Centre Code</th>"+
                          "<th>Agent Code</th>"+
                          "<th>Film Code</th>"+
                          "<th>Ticket number</th>"+
                          "<th>Phone</th>"+
                          "<th>Date of Issue</th>"+
                          "<th>Time of Issue</th>"+
                          "<th>Ticket rate</th>"+
                          "<th>Status</th></tr>";

            switch (result_text.toLowerCase()) {
                case "success":
                    //alert('Successful query');
                    agents=xmlDoc.getElementsByTagName("customer");
                    for (i=0;i<agents.length;i++) {
                        table_text = table_text + "<tr>";
                        try {
                            table_text = table_text + insertField(agents[i], "cust_id");
                            table_text = table_text + insertField(agents[i], "cust_name");
                            table_text = table_text + insertField(agents[i], "centre_code");
                            table_text = table_text + insertField(agents[i], "agent_code");
                            table_text = table_text + insertField(agents[i], "film_code");
                            table_text = table_text + insertField(agents[i], "ticket_no");
                            table_text = table_text + insertField(agents[i], "phone");
                            table_text = table_text + insertField(agents[i], "date_issue");
                            table_text = table_text + insertField(agents[i], "time_issue");
                            table_text = table_text + insertField(agents[i], "ticket_rate");
                            table_text = table_text + insertField(agents[i], "value");
                        } catch (er) {
                            alert('Invalid ticket number');
                        }
                        table_text = table_text + "</tr>";
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }

            table_text = table_text + "</table>";
            document.getElementById("cust_table").innerHTML=table_text;
        }

        function validateForm() {
            var return_value = true;
            var films=document.getElementById("film_code");

            if (!films.selectedIndex) {
                document.getElementById("film_code_hint").innerHTML =
                    "<p style='color:red'><b>Film Code should be selected</b></p>";
                return_value = false;
            } else {
                document.getElementById("film_code_hint").innerHTML = " ";
            }

            return return_value;

        }

        function displayTable() {
            if (!validateForm()) {
                return;
            }
            var films=document.getElementById("film_code");
            var film_code=films.options[films.selectedIndex].value;
            var ticket_rates=document.getElementById("ticket_rate");
            var ticket_rate=ticket_rates.options[ticket_rates.selectedIndex].value;

            if(!ticket_rates.selectedIndex) {
                srvr_request="querycustomer.php?film_code="+film_code;
            } else {
                srvr_request="querycustomer.php?film_code="+film_code+
                                                "&ticket_rate="+ticket_rate;
            }
            ajaxCall(srvr_request,dispTableHdlr);
        }

    </script>

  </head>
  <body onLoad="initFilmSelect()">
      <table border="1">
          <tr><td width="150"><a href="producer.html"><b>Producers</b></a></td>
              <td width="150"><a href="film.html"><b>Films</b></a></td>
              <td width="150"><a href="centre.html"><b>Centres</b></a></td>
              <td width="150"><a href="agent.html"><b>Agents</b></a></td>
              <td width="150"><a href="customer.html"><b>Tickets</b></a></td>
              <td width="150"><a href="ticket.html"><b>Ticket rate information</b></a></td>
              <td width="150"><a href="ticketstatus.html"><b>Status of ticket sales for each film</b></a></td>
              <td width="150"><a href="customerstatus.html"><b>Status for each customer</b></a></td>
              <td width="150"><a href="logout.php"><b>Logout</b></a></td></tr>
      </table>

        <table border="0">

            <tr><td><label>Film Code&nbsp;*</label></td>
                <td><select id=film_code><option value=''></option></select></td>
                <td><div id="film_code_hint"> </div></td></tr>

            <tr><td><label>Ticket Rate&nbsp;*</label></td>
                <td><select id=ticket_rate><option value=''></option></select></td>
                <td><div id="ticket_rate_hint"> </div></td></tr>

            <tr /> <tr /> <tr />
        </table>
        <div id="cust_table">
            <button onclick="displayTable()">Show all tickets for this film</button>
        </div>

  </body>
</html>
